package com.atguigu.yygh.user.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.user.service.PcService;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.atguigu.yygh.vo.user.RegisterVo;
import com.mongodb.client.result.UpdateResult;
import com.netflix.client.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.util.List;
import java.util.Map;

@Service
public class PcServiceImpl implements PcService {
    @Autowired
    private MongoTemplate mongoTemplate;
    boolean flag = false;
    @Override
    public int save(String phone, String code) {

        Query query = new Query(Criteria
                .where("mobile").is(phone));
        List<RegisterVo> registerVos = mongoTemplate.find(query, RegisterVo.class);
        if (registerVos.size()==0) {
            RegisterVo registerVo = new RegisterVo();
            registerVo.setMobile(phone);
            registerVo.setCode(code);
            RegisterVo insert = mongoTemplate.insert(registerVo);
            if (insert != null) {
                flag = true;
            }
        } else {
            Update update = new Update();
            update.set("code", code);
            UpdateResult result = mongoTemplate.upsert(query, update, RegisterVo.class);
            if (result != null) {
                flag = true;
            }
        }
        if(flag){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public boolean send(MsmVo msmVo) {
        if(!StringUtils.isEmpty(msmVo.getPhone())) {
            String code = (String)msmVo.getParam().get("code");
            int save = this.save(msmVo.getPhone(), code);
            if(save==1){
                return true;
            }else {
                return false;
            }

        }
        return false;
    }

    //mq发送短信封装
    @Override
    public boolean send1(MsmVo msmVo) {
        if(!org.springframework.util.StringUtils.isEmpty(msmVo.getPhone())) {
            boolean isSend = this.send(msmVo.getPhone(), msmVo.getParam());
            return isSend;
        }
        return false;
    }
    private boolean send(String phone, Map<String,Object> param) {
        //判断手机号是否为空
        if(org.springframework.util.StringUtils.isEmpty(phone)) {
            return false;
        }
        //整合阿里云短信服务
        //设置相关参数
        //调用方法进行短信发送
        try {
            System.out.println("发送给"+phone+":"+param.get("title")+"预约日期："+param.get("reservDate")+"就诊人："+param.get("name"));
            return true;
        } catch (Exception e){
            e.printStackTrace();
        }

        return false;
    }

}
